/**
 * 
 */
package com.gr.staffpm.pages.search;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.model.DetachableTaskModel;
import com.gr.staffpm.tasks.model.TaskLink;
import com.gr.staffpm.tasks.service.TaskService;
import com.gr.staffpm.widget.navigator.StaffPMNavigator;


/**
 * @author Graham Rhodes 28 Feb 2011 03:18:12
 */
@MountHybrid
@MountPath(path = "secure/QuickSearch")
public class QuickSearch extends StaffPMWebPage {

    @SpringBean(name = "taskService")
    private TaskService taskService;
    @SpringBean(name = "userService")
    private UserService userService;

    public QuickSearch(final List<Task> tasks) {

        add(new FeedbackPanel("feedback"));

        SortableDataProvider<Task> dp = new SortableDataProvider<Task>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Iterator<? extends Task> iterator(int first, int count) {
                return tasks.subList(first, first + count).iterator();
            }

            @Override
            public int size() {
                return tasks.size();
            }

            @Override
            public IModel<Task> model(Task task) {
                return new DetachableTaskModel(taskService, task);
            }

        };

        final DataView<Task> dataView = new DataView<Task>("tasks", dp) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(Item<Task> item) {
                final Task task = item.getModelObject();
                item.add(new TaskLink("nameLink", item.getModel(), userService).add(new Label("name", task.getName())));
                item.add(new Label("summary", task.getSummary()));
                item.add(new Label("assignee", task.getAssignee().getFullName()));
            }
        };
        dataView.setItemsPerPage(10);

        add(new OrderByBorder("orderByTaskName", Task.NAME, dp));
        add(new OrderByBorder("orderByAssignee", Task.ASSIGNEE, dp));

        add(dataView);
        add(new StaffPMNavigator("navigator", dataView));
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.StaffPMWebPage#getTitle()
     */
    @Override
    public String getTitle() {
        return "Quick Search - StaffPM";
    }

}
